package com.bobo.medicinechest;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bobo.base.BaseActivity;
import com.bobo.medicinechest.adapter.GoodsListAdapter;
import com.bobo.medicinechest.adapter.GoodsTypeAdapter;
import com.bobo.medicinechest.bean.GoodsBean;
import com.bobo.medicinechest.bean.GoodsTypeBean;
import com.bobo.medicinechest.databinding.ActivityGoodsListBinding;
import com.bobo.medicinechest.dialog.DrugDetailsDialog;
import com.bobo.medicinechest.dialog.ShopCarDialog;
import com.bobo.medicinechest.manager.CartManager;
import com.bobo.medicinechest.util.PriceFormatUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.XPopup;
import com.xuexiang.xui.widget.divider.GradSpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class GoodsListActivity extends BaseActivity<ActivityGoodsListBinding> {

    private GoodsTypeAdapter mGoodsTypeAdapter;
    private GoodsListAdapter mGoodsListAdapter;
    //商品总列表
    private List<GoodsTypeBean> goodsTypeBeanList = new ArrayList<>();
    //商品二级列表
    private List<GoodsBean> mGoodsList = new ArrayList<>();
    private List<GoodsBean> mShopCarList = new ArrayList<>();
    private int mShopCarNumber;

    @Override
    protected ActivityGoodsListBinding getViewBinding() {
        return ActivityGoodsListBinding.inflate(getLayoutInflater());
    }

    @Override
    protected ViewGroup initTitle() {
        return null;
    }

    @Override
    protected void initViews() {
        List<GoodsBean> goodsList = getData();
        initRVType(goodsTypeBeanList);

        mGoodsList.addAll(goodsTypeBeanList.get(0).getGoods());
        //全部tab
        for (int i = 0; i < goodsTypeBeanList.size(); i++) {
            List<GoodsBean> goods = goodsTypeBeanList.get(0).getGoods();
            if (!goods.isEmpty()) {
                initRVGoods(goodsList);
            }
        }
        //购物车
        binding.flBuyCar.setOnClickListener(v -> {
            for (int i = 0; i < mShopCarList.size(); i++) {
                LogUtils.e(mShopCarList.get(i).getName() + " Number="+ mShopCarList.get(i).getNumber());
            }
            ShopCarDialog chargeDeviceDialog = new ShopCarDialog(GoodsListActivity.this, mShopCarList);
            new XPopup.Builder(GoodsListActivity.this)
                    .moveUpToKeyboard(false)
                    .asCustom(chargeDeviceDialog)
                    .show();
        });

    }

    @Override
    protected void initListeners() {

    }

    @Override
    protected void initData() {

    }

    private void initRVType(List<GoodsTypeBean> list) {
        binding.rvType.setLayoutManager(new LinearLayoutManager(GoodsListActivity.this));
        mGoodsTypeAdapter = new GoodsTypeAdapter(list);
        binding.rvType.setAdapter(mGoodsTypeAdapter);

        //分类item点击
        mGoodsTypeAdapter.setOnItemClickListener((adapter, view, position) -> {
            GoodsTypeBean goodsListBean = mGoodsTypeAdapter.getData().get(position);
            LogUtils.e("点了id=" + goodsListBean.getId());

            for (int i = 0; i < goodsTypeBeanList.size(); i++) {
                if (i == position) {
                    goodsTypeBeanList.get(i).setSelect(true);
                } else {
                    goodsTypeBeanList.get(i).setSelect(false);
                }
            }
            mGoodsTypeAdapter.notifyDataSetChanged();

            //商品列表
//            if (position == 0) {
//                mGoodsListAdapter.setNewData(allGoodsList);
//            } else {
            mGoodsList = goodsListBean.getGoods();
            mGoodsListAdapter.setNewData(mGoodsList);
//            }

        });
    }

    private void initRVGoods(List<GoodsBean> list) {
        binding.rvGoods.setLayoutManager(new GridLayoutManager(GoodsListActivity.this, 3));
        if (binding.rvGoods.getItemDecorationCount() == 0) {
            binding.rvGoods.addItemDecoration(new GradSpaceItemDecoration(ConvertUtils.dp2px(7), ConvertUtils.dp2px(7), false));
        }
        mGoodsListAdapter = new GoodsListAdapter(list);
        binding.rvGoods.setAdapter(mGoodsListAdapter);

        mGoodsListAdapter.setOnItemClickListener((adapter, view, position) -> {
            DrugDetailsDialog drugDetailsDialog = new DrugDetailsDialog(GoodsListActivity.this, mShopCarList);
            new XPopup.Builder(GoodsListActivity.this)
                    .moveUpToKeyboard(false)
                    .asCustom(drugDetailsDialog)
                    .show();

        });

        mGoodsListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            GoodsBean goodsBean = mGoodsListAdapter.getData().get(position);
            switch (view.getId()) {
                case R.id.iv:

                    break;
                case R.id.ivAdd:
                    if (goodsBean.getNumber() == 0) {
                        ToastUtils.showLong("购买数量不能超过库存");
                        return;
                    }

                    for (int i = 0; i < mGoodsList.size(); i++) {
                        if (TextUtils.equals(goodsBean.getId(), mGoodsList.get(i).getId())) {
                            mGoodsList.get(i).setNumber(goodsBean.getNumber() - 1);
                            mGoodsListAdapter.notifyItemChanged(i);
                        }
                    }

                    addGoodsToCart(goodsBean);
                    CartManager.getInstance().addGoodsToCart(goodsBean);

                    mShopCarNumber++;
                    if (mShopCarNumber > 0) {
                        binding.tvNum.setText(String.valueOf(mShopCarNumber));
                        binding.tvNum.setVisibility(View.VISIBLE);
                    } else {
                        binding.tvNum.setVisibility(View.GONE);
                    }

                    binding.tvTotalPrice.setText(PriceFormatUtil.formatPrice(CartManager.getInstance().getTotalPrice(), 19, 31, 22));
                    break;
            }
        });
    }

    /**
     * 添加商品到购物车
     * @param goodsBean 要添加的商品
     */
    private void addGoodsToCart(GoodsBean goodsBean) {
        for (GoodsBean cartGoods: mShopCarList) {
            if (TextUtils.equals(goodsBean.getId(), cartGoods.getId())) {
                //购物车存在该商品
                cartGoods.setNumber(cartGoods.getNumber() + 1);
                return;
            }
        }

//        for (int i = 0; i < mShopCarList.size(); i++) {
//            if (TextUtils.equals(goodsBean.getId(), mShopCarList.get(i).getId())) {
//                //购物车存在该商品
//                mShopCarList.get(i).setNumber(mShopCarList.get(i).getNumber() + 1);
//                return;
//            }
//        }
        GoodsBean newGoods = new GoodsBean();
        newGoods.setId(goodsBean.getId());
        newGoods.setName(goodsBean.getName());
        newGoods.setType(goodsBean.getType());
        newGoods.setPrice(goodsBean.getPrice());
        newGoods.setImage(goodsBean.getImage());
        newGoods.setNumber(goodsBean.getNumber());
        newGoods.setBuyNumber(1);  //todo
        mShopCarList.add(newGoods);
    }

    @NonNull
    private List<GoodsBean> getData() {
        GoodsTypeBean goodsTypeBean;
        goodsTypeBean = new GoodsTypeBean();
        goodsTypeBean.setId("1");
        goodsTypeBean.setName("泄药");
        goodsTypeBean.setSelect(true);

        List<GoodsBean> goodsList = new ArrayList<>();
        GoodsBean goodsBean;
        for (int i = 0; i < 5; i++) {
            goodsBean = new GoodsBean();
            goodsBean.setId(i + "");
            goodsBean.setName("泻立停" + i);
            goodsBean.setNumber(i + 1);
            goodsBean.setPrice("0.1" + i);
            goodsBean.setType("1");
            goodsList.add(goodsBean);
        }
        goodsTypeBean.setGoods(goodsList);
        goodsTypeBeanList.add(goodsTypeBean);

        List<GoodsBean> goodsList1 = new ArrayList<>();
        goodsTypeBean = new GoodsTypeBean();
        goodsTypeBean.setId("2");
        goodsTypeBean.setName("感冒用药");
        goodsTypeBean.setSelect(false);
        for (int i = 0; i < 4; i++) {
            goodsBean = new GoodsBean();
            goodsBean.setId(i + "");
            goodsBean.setName("小儿葵花颗粒" + i);
            goodsBean.setNumber(Integer.parseInt("2" + i));
            goodsBean.setPrice("0.2" + i);
            goodsBean.setType("1");
            goodsList1.add(goodsBean);
        }
        goodsTypeBean.setGoods(goodsList1);
        goodsTypeBeanList.add(goodsTypeBean);

        List<GoodsBean> goodsList2 = new ArrayList<>();
        goodsTypeBean = new GoodsTypeBean();
        goodsTypeBean.setId("3");
        goodsTypeBean.setName("肛肠用药");
        goodsTypeBean.setSelect(false);
        for (int i = 0; i < 6; i++) {
            goodsBean = new GoodsBean();
            goodsBean.setId(i + "");
            goodsBean.setName("肛泰" + i);
            goodsBean.setNumber(Integer.parseInt("3" + i));
            goodsBean.setPrice("0.3" + i);
            goodsBean.setType("1");
            goodsList2.add(goodsBean);
        }
        goodsTypeBean.setGoods(goodsList2);
        goodsTypeBeanList.add(goodsTypeBean);


        for (int i = 0; i < goodsTypeBeanList.size(); i++) {
            for (int j = 0; j < goodsTypeBeanList.get(i).getGoods().size(); j++) {
                List<GoodsBean> goods = goodsTypeBeanList.get(i).getGoods();
                LogUtils.e(goods.get(j).getName());
            }
        }
        return goodsList;
    }
}
